package Model.CSVReport;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * CSV文件导出工具类
 *
 * Created on 2017-01-07
 */
public class CSVUtils {
    public static boolean CSVCreate(String Path,String[] head,List<String[]> datalist){
        boolean isSucess = false;
        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            File file = new File(Path);
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out,"GB2312");
            bw = new BufferedWriter(osw,1024);
            File parent = file.getParentFile();
            if (parent !=null && !parent.exists()){
                parent.mkdir();
            }
            file.createNewFile();
            writeRow(head,bw);
            if (datalist.size() !=0 && datalist != null){
                for (String[] strings : datalist){
                    writeRow(strings,bw);
                }
                bw.flush();
            }
            isSucess = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSucess;
    }

    private static void writeRow(String[] row, BufferedWriter csvWriter) throws IOException {
        // 写入文件头部
        for (String s : row) {
            StringBuffer sb = new StringBuffer();
            String rowStr = sb.append("\"").append(s).append("\",").toString();
            csvWriter.write(rowStr);
        }
        csvWriter.newLine();
    }

    public static List<String> importCsv(String Path){
        List<String> dataList=new ArrayList<>();
        File file = new File(Path);
        BufferedReader br=null;
        try {
            DataInputStream in = new DataInputStream(new FileInputStream(file));
            br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dataList;
    }
}
